<?php

class Admin_AreaController extends Zend_Controller_Action
{    
	private $_params;
	private $_auth;	
    public function init(){        
        $this->_params      = $this->_request->getParams();        
        $this->_auth         = new My_Auth();
        $this->view->auth    = $this->_auth;        
        $this->_auth        = $this->_auth->checkAuthentication();
        $this->view->auth   = $this->_auth;        
    }
    public function indexAction(){        
        $modelArea  = new Admin_Model_Area();        
        $areaList   = $modelArea->getAllData()->toArray();                     
        $this->view->areaList = $areaList;        
    }    
    public function deleteAction(){
        $proId = $this->_getParam('id', 0);
        $modelPro = new Admin_Model_Area();
        $proCheck = $modelPro->getDataId($proId);
        if($proCheck){
            $this->view->proCheck = $proCheck;
            if($this->getRequest()->isPost()){
                $del = $this->getRequest()->getPost('del');
                if($del == 'Có'){
                    $proId = $this->getRequest()->getPost('id');
                    $modelProduct= new Admin_Model_Product();
                    $idCheckItem= $modelProduct->getDataByAreaId($proId)->toArray();
                    if(isset($idCheckItem) && $idCheckItem){
                        $this->view->mess = "Không thể xóa vì còn phần tử con";
                        $this->view->flag = "alert";
                        return;
                    }
                    else{
                        $modelPro->deleteData($proId); 
                        $this->_helper->redirector('index', 'area', 'admin');
                    }                         
                }
                
            }
        }
    }
    public function addAction(){
        $form = new Admin_Form_Area();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;                
        if ($this->getRequest()->isPost()) {            
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                
                $addData['name']    = $form->getValue('name');
                $addData['ord']     = $form->getValue('ord');
                $addData['display'] = $form->getValue('display');                
                
                $modelArea = new Admin_Model_Area();			
				$addId = $modelArea->addData($addData);
                if($addId){
                    $this->_helper->redirector('index', 'area', 'admin');                           
                }else{
                    $this->view->mess = "Đã xảy ra lỗi; Vui lòng thử lại sau!!";
                    $this->view->flag = "alert";
                }                    			
            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Admin_Form_Area();
        $modelArea = new Admin_Model_Area();
		$form->submit->setLabel('Lưu thay đổi');
		$this->view->form = $form;
        $id = (int)($this->_getParam('id', 0));
        $proCheck = $modelArea->getDataId($id);
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$dataUpdate['id'] = (int)($formData['id']);
				$dataUpdate['name'] = ($formData['name']);
                $dataUpdate['ord'] = (int)($formData['ord']);
				$dataUpdate['display'] = ($formData['display']);
                if($proCheck){  
				    $modelArea->updateData($id, $dataUpdate);
                    $this->_helper->redirector('index', 'area', 'admin'); 
                }else{
                    $this->view->mess ="Không tồn tại danh mục này!!!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}

			}else{
				$form->populate($formData);
			}
		}else{
		    $form->populate($proCheck->toArray());
		}
    }
}